/**
 * @brief SSL连接
 * @version 1.0
 * @author 宋炜
 */

#pragma once
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "mallocSharedPtr.hpp"
#include "sslSvrItfc.hpp"

class sslSvrConnection
{
public:
	using buff_t = wheels::mallocSharedPtr<uint8_t >;
private:

#if defined( WIN32 ) || defined( WINNT )
#    include <windows.h>
    using native_socket = SOCKET;
    native_socket  m_sock__;
#elif defined( __LINUX__ )
    using native_socket = int;
	int     m_sock__;
#endif
    std::shared_ptr< SSL >        pt_ssl__;
private:

public:
    sslSvrConnection( native_socket sock , SSL * ssl ):m_sock__( sock ), pt_ssl__( ssl , []( SSL * ssl){
            if( ssl ){
                SSL_free( ssl );
            }
        })
    {
        if( !ssl ){ throw std::runtime_error( "SSL指针为空" );}
    }

    virtual ~sslSvrConnection(){
        ::close( m_sock__ );
        auto dispt = sslSvrItfc::dispatcher_t::get();
		if( dispt ){
			dispt->emit( sslSvrItfc::event::EVT_CLOSE , std::string() );
		}
	}

	buff_t read();
	size_t send( buff_t buff );
};
